package com.ss.warfarin.persistence.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServlet;

import com.ss.warfarin.common.util.DbUtils;
import com.ss.warfarin.persistence.model.Patient;

public class ProfileDao extends HttpServlet {

	private static final long serialVersionUID = 7450366913852818405L;

	public Patient searchPatient(String patientId) {
		Patient patient = null;

		try {

			Connection connection = DbUtils.getConnection();

			// Query database
			String sql = " SELECT patient_profile_id ,patient_name, patient_lastname ,patient_id, citizen_id, patient_birthday ,address ,phon_number,gender , congenital_disease FROM hos_patient_profile WHERE patient_id = ?";

			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, patientId);
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				patient = new Patient();
				patient.setPatientProfileId(rs.getInt("patient_profile_id"));
				patient.setPatientName(rs.getString("patient_name"));
				patient.setPatientLastname(rs.getString("patient_lastname"));
				patient.setPatientBirthday(rs.getDate("patient_birthday"));
				patient.setAddress(rs.getString("address"));
				patient.setPatientId(rs.getString("patient_id"));
				patient.setPatientId(rs.getString("citizen_id"));
				patient.setPhonNumber(rs.getString("phon_number"));
				patient.setGender(rs.getString("gender"));
				patient.setCongenitalDisease(rs.getString("congenital_disease"));
			}

			ps.close();
			rs.close();
			DbUtils.closeConnection(connection);

		} catch (Exception e) {
			System.out.println("ERROR:" + e.getMessage());
		}

		return patient;

	}

}
